﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace InsertObject
{
    public partial class frmMain : Form
    {
        string filename = "";
        string filename2 = "";
        string filename3 = "";
        bool m_secondfile = false;
        bool m_thirdfile = false;
        bool m_edit = false;
        PluginSettings m_settings;

        public frmMain(PluginSettings settings)
        {
            InitializeComponent();
            m_settings = settings;
            IsEdit();
        }

        public frmMain(PluginSettings settings, bool edit)
        {
            InitializeComponent();
            m_settings = settings;
            m_edit = edit;
            IsEdit();
        }
        
        private void IsEdit()
        {
            if (m_edit)
            {
                txbxFileName1.Text = m_settings.Filename;
                txbxFileName2.Text = m_settings.Filename2;
                txbxFileName3.Text = m_settings.Filename3;
                txbxLabel1.Text = m_settings.Label;
                txbxLabel2.Text = m_settings.Label2;
                txbxLabel3.Text = m_settings.Label3;
                txbxLinkText1.Text = m_settings.LinkText;
                txbxLinkText2.Text = m_settings.LinkText2;
                txbxLinkText3.Text = m_settings.LinkText3;
                chbxOpenLink1.Checked = m_settings.NewWindow;
                chbxOpenLink2.Checked = m_settings.NewWindow2;
                chbxOpenLink3.Checked = m_settings.NewWindow3;
                chbxFTP.Checked = m_settings.UseFTP;
                if (m_settings.Filename2 != string.Empty)
                {
                    btnAdd1_Click(null, null);
                    m_secondfile = true;
                }
                if (m_settings.Filename3 != string.Empty)
                {
                    btnAdd2_Click(null, null);
                    m_thirdfile = true;
                }
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                filename = openFileDialog1.FileName;
                txbxFileName1.Text = filename;
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            filename = txbxFileName1.Text;
            filename2 = txbxFileName2.Text;
            filename3 = txbxFileName3.Text;

            if (filename == string.Empty || filename == string.Empty)
            {
                MessageBox.Show("Sorry, you must put in some text to appear as the link and a valid file", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!File.Exists(filename))
            {
                MessageBox.Show("Sorry, " + filename + " does not exist", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            m_settings.Filename = filename;
            m_settings.Label = txbxLabel1.Text;
            m_settings.LinkText = txbxLinkText1.Text;
            m_settings.NewWindow = chbxOpenLink1.Checked;
            if (m_secondfile)
            {
                if (filename2 == string.Empty || filename2 == string.Empty)
                {
                    MessageBox.Show("Sorry, you must put in some text to appear as the link for the second file and a valid file", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (!File.Exists(filename2))
                {
                    MessageBox.Show("Sorry, " + filename2 + " does not exist", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                m_settings.Filename2 = filename2;
                m_settings.Label2 = txbxLabel2.Text;
                m_settings.LinkText2 = txbxLinkText2.Text;
                m_settings.NewWindow2 = chbxOpenLink2.Checked;
            }
            if (m_thirdfile)
            {
                if (filename3 == string.Empty || filename3 == string.Empty)
                {
                    MessageBox.Show("Sorry, you must put in some text to appear as the link for the third file and a valid file", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (!File.Exists(filename3))
                {
                    MessageBox.Show("Sorry, " + filename3 + " does not exist", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                m_settings.Filename3 = filename3;
                m_settings.Label3 = txbxLabel3.Text;
                m_settings.LinkText3 = txbxLinkText3.Text;
                m_settings.NewWindow3 = chbxOpenLink3.Checked;
            }
            m_settings.UseFTP = chbxFTP.Checked;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnAdd1_Click(object sender, EventArgs e)
        {
            DoFile2(true);
            btnInsert.Location = new System.Drawing.Point(493, 203);
            btnCancel.Location = new Point(574, 203);
            this.Height = 266;

        }

        private void DoFile2(bool x)
        {
            txbxFileName2.Visible = x;
            txbxLabel2.Visible = x;
            txbxLinkText2.Visible = x;
            btnAdd2.Visible = x;
            btnMinus2.Visible = x;
            btnBrowse2.Visible = x;
            chbxOpenLink2.Visible = x;
            btnAdd1.Enabled = !x;
            m_secondfile = x;
        }

        private void DoFile3(bool x)
        {
            txbxFileName3.Visible = x;
            txbxLabel3.Visible = x;
            txbxLinkText3.Visible = x;
            btnAdd3.Visible = x;
            btnMinus3.Visible = x;
            btnBrowse3.Visible = x;
            chbxOpenLink3.Visible = x;
            btnAdd2.Enabled = !x;
            m_thirdfile = x;
        }

        private void btnMinus2_Click(object sender, EventArgs e)
        {
            if (m_thirdfile)
            {
                txbxFileName2.Text = txbxFileName3.Text;
                txbxLabel2.Text = txbxLabel3.Text;
                txbxLinkText2.Text = txbxLinkText3.Text;
                txbxLinkText3.Text = string.Empty;
                txbxLabel3.Text = string.Empty;
                txbxFileName3.Text = string.Empty;
                chbxOpenLink2.Checked = chbxOpenLink3.Checked;
                btnMinus3_Click(this, e);
            }
            else
            {
                DoFile2(false);
                btnInsert.Location = new System.Drawing.Point(493, 153);
                btnCancel.Location = new Point(574, 153);
                this.Height = 216;
                txbxFileName2.Text = string.Empty;
                txbxLabel2.Text = string.Empty;
                txbxLinkText2.Text = string.Empty;
                chbxOpenLink2.Checked = true; 
                m_settings.Filename2 = string.Empty;
                m_settings.Label2 = string.Empty;
                m_settings.LinkText2 = string.Empty;
            }
        }

        private void btnAdd2_Click(object sender, EventArgs e)
        {
            DoFile3(true);
            btnInsert.Location = new System.Drawing.Point(493, 253);
            btnCancel.Location = new Point(574, 253);
            this.Height = 316;
        }

        private void btnMinus3_Click(object sender, EventArgs e)
        {
            DoFile3(false);
            btnAdd1_Click(this, e);
            txbxFileName3.Text = string.Empty;
            txbxLabel3.Text = string.Empty;
            txbxLinkText3.Text = string.Empty;
            chbxOpenLink3.Checked = true;
            m_settings.Filename3 = string.Empty;
            m_settings.Label3 = string.Empty;
            m_settings.LinkText3 = string.Empty;
        }

        private void btnBrowse2_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                filename2 = openFileDialog1.FileName;
                txbxFileName2.Text = filename2;
            }
        }

        private void btnBrowse3_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                filename3 = openFileDialog1.FileName;
                txbxFileName3.Text = filename3;
            }
        }
    }
}